package UseFile;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileReadWriteExample {
    public static void main(String[] args) {
        // 使用 FileWriter 写入数据
        try (FileWriter writer = new FileWriter("f:/output.txt")) {
            writer.write("hello world");
            System.out.println("数据写入成功！");
        } catch (IOException e) {
            System.out.println("写入数据时发生错误: " + e.getMessage());
        }

        // 使用 FileReader 读取数据
        try (FileReader reader = new FileReader("f:/output.txt")) {
            int data;
            StringBuilder content = new StringBuilder();
            while ((data = reader.read()) != -1) {
                content.append((char) data);
            }
            System.out.println("读取到的数据是: " + content.toString());
        } catch (IOException e) {
            System.out.println("读取数据时发生错误: " + e.getMessage());
        }
    }
}
